package com.uniapp.mealserver.controller;

import com.uniapp.mealserver.controller.utils.R;
import com.uniapp.mealserver.domain.Meal;
import com.uniapp.mealserver.service.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/meals")
public class MealController {

    @Autowired
    private MealService mealService;

    @GetMapping
    public R getMeals() {
        return new R(mealService.getAll(),"查询成功");
    }

    @PostMapping
    public R addMeal(@RequestBody Meal meal) {
        boolean result = mealService.save(meal);
        if(result) {
            return new R(result,"新增成功");
        }
        return new R("新增失败");
    }

    @PutMapping
    public R updateMeal(@RequestBody Meal meal) {
        boolean result = mealService.update(meal);
        if(result) {
            return new R(result,"更新成功");
        }
        return new R("更新失败");
    }

    @DeleteMapping("{id}")
    public R deleteMeal(@PathVariable Long id) {
        boolean result = mealService.delete(id);
        if(result) {
            return new R(result,"删除成功");
        }
        return new R("删除失败");
    }

    @GetMapping("{id}")
    public R getMealById(@PathVariable Long id) {
        return new R(mealService.getById(id),"查询成功");
    }

    @GetMapping("{currentPage}/{pageSize}")
    public R getMealByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize,Meal meal) {
        return new R(mealService.getPage(currentPage,pageSize,meal),"查询成功");
    }
}
